<!--  线 + 柱混合图 -->
<template>
    <div :id="id" :class="className" :style="{ height, width }"></div>
</template>

<script setup lang="ts">
import * as echarts from "echarts";

const props = defineProps({
  id: {
    type: String,
    default: "lineChart",
  },
  className: {
    type: String,
    default: "",
  },
  width: {
    type: String,
    default: "100%",
    required: true,
  },
  height: {
    type: String,
    default: "300px",
    required: true,
  },
});

const options = {
  grid: {
          containLabel: true,
          left: 20,
          right: 40,
          top: 40,
          bottom: 20
        },
        tooltip: {
          trigger: 'axis', // 设置触发类型为坐标轴
          formatter: function(params) {
            // 自定义 tooltip 的内容
            var result = params[0].name + '<br>'
            params.forEach(function(item) {
              result += item.seriesName + ': ' + item.value + '<br>'
            })
            return result
          }
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: [10, 20, 30, 40, 50, 60],
          axisLabel: {
            color: 'rgba(153, 153, 153, 1)' // 设置x轴标签颜色为灰色
          },
          axisLine: {
            lineStyle: {
              color: 'rgba(153, 153, 153, 1)' // 设置x轴线条颜色为灰色
            }
          }
        },
        yAxis: {
          type: 'value',
          axisLabel: {
            color: 'rgba(153, 153, 153, 1)' // 设置x轴标签颜色为灰色
          },
          axisLine: {
            lineStyle: {
              color: 'rgba(153, 153, 153, 1)' // 设置x轴线条颜色为灰色
            }
          }
        },
        series: [{
          data: [20, 50, 30, 40, 50, 20],
          type: 'line',
          showSymbol: false,
          smooth: true,
          itemStyle: {
            color: '#37e196'
          },
          areaStyle: {
            color: {
              type: 'linear',
              x: 0,
              y: 0,
              x2: 0,
              y2: 1,
              colorStops: [{
                offset: 0,
                color: 'rgba(55, 225, 150,0.5)' // 0% 处的颜色
              }, {
                offset: 1,
                color: 'rgba(55, 225, 150,0)' // 100% 处的颜色
              }],
              global: false // 缺省为 false
            }
          }
        }]
  
};
const chart = ref<any>("");
onMounted(() => {
  // 图表初始化
  chart.value = markRaw(
    echarts.init(document.getElementById(props.id) as HTMLDivElement)
  );

  chart.value.setOption(options);

  // 大小自适应
  window.addEventListener("resize", () => {
    chart.value.resize();
  });
});
const downloadEchart = () => {
  // 获取画布图表地址信息
  const img = new Image();
  img.src = chart.value.getDataURL({
    type: "png",
    pixelRatio: 1,
    backgroundColor: "#fff",
  });
  // 当图片加载完成后，生成 URL 并下载
  img.onload = () => {
    const canvas = document.createElement("canvas");
    canvas.width = img.width;
    canvas.height = img.height;
    const ctx = canvas.getContext("2d");
    if (ctx) {
      ctx.drawImage(img, 0, 0, img.width, img.height);
      const link = document.createElement("a");
      link.download = `业绩柱状图.png`;
      link.href = canvas.toDataURL("image/png", 0.9);
      document.body.appendChild(link);
      link.click();
      link.remove();
    }
  };
};
</script>
<style lang="scss" scoped>
.title {
  display: flex;
  justify-content: space-between;

  .download {
    cursor: pointer;

    &:hover {
      color: #409eff;
    }
  }
}
</style>
